package com.zxy.controller;

import com.aimao.model.OrderIng;
import com.aimao.model.OrderOk;
import com.zxy.feignclients.NacosAClient;
import com.zxy.feignclients.OrderServiceCilent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
//@RefreshScope
@Slf4j
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private NacosAClient nacosAClient;
    @Autowired
    private OrderServiceCilent orderServiceCilent;

    @GetMapping("/test")
    public String callNacosA(){
        //如果RestTemplate 这个份Bean 有@LoadBalanced ，RestTemplate在执行的时候，就会增加一个拦截器
        // 这个拦截器，把 一级目录作为服务名，去Nacos找出对应的ip和端口，192.168.14.8:8080,192.168.14.8:9090
        String forObject = restTemplate.getForObject("http://nacos-a/port", String.class);
        return   forObject;
    }
    @GetMapping("/openfeign")
    public String callFeignNacosA(){
        log.debug("开始调用nacos-app");
        String port = nacosAClient.getPort();
        return port;
    }

    @GetMapping("/calluser")
    public String calluser(@RequestParam Integer id){
        String user = nacosAClient.getUser(id);
        return user;
    }

    @GetMapping("/sleep")
    public String sleep(@RequestParam Integer s){
        String body = nacosAClient.sleep(s);
        return body;
    }

    @GetMapping("/ordering")
    public OrderOk ordering(){
        OrderIng orderIng = new OrderIng();
        orderIng.setUserId(55);
        return orderServiceCilent.order(orderIng);
    }
}
